/**
 * This program is free software but WITHOUT ANY WARRANTY OR CONDITIONS OF ANY KIND.
 *
 * Copyright (C) 2008-2013 www.pretty-tools.com
 */

var gLogger = null;
try
{
    gLogger = yrprxFactory.createLogger("wsshOptionsDlg");
}
catch(ex)
{
    dump("Cannot create logger! Exception: " + yrprxBrowser.exceptionToString(ex) + "\n");
}

function onLoad()
{
    try
    {
        var rule = window.arguments[0];
        if (rule.domain) document.getElementById("domain").value = rule.domain;

        var page = rule.page;
        if (page)
        {
            if ("edit" == rule.mode)
            {
                var wnd = document.getElementById("wsshRuleDlg");
                if (wnd) document.title = wnd.getAttribute("edit_title");
            }

            if (page.pattern) document.getElementById("page_pattern").value = page.pattern.source;

            if (page.elementTag) document.getElementById("element_tag").value = page.elementTag;
            if (page.elementPattern) document.getElementById("element_pattern").value = page.elementPattern.source;

            if (page.elementTemplate)
            {
                var elementTemplate = document.getElementById("element_template");
                elementTemplate.value = page.elementTemplate;

                if (page.isJavaScriptTemplate())
                    makeMultiline();
            }
        }
    }
    catch(ex)
    {
        if (gLogger.enabled) gLogger.error("onLoad: ", ex);
    }
}

function doError(elem)
{
    if (yrprxBrowser.hasAttribute(elem, "message_on_empty"))
        yrprxPrompt.alert(document.title, elem.getAttribute("message_on_empty"));
    else
        yrprxPrompt.alert(document.title, wsshResources["webslideshow.error.enter.correct.value"]);

    elem.focus();

    throw "Empty value";
}

function doRegExpError(elem, exception)
{
    yrprxPrompt.alert(document.title, wsshResources["webslideshow.error.enter.correct.value"] + "\n" + yrprxBrowser.exceptionToString(exception));

    elem.focus();

    throw exception;
}

function checkNotEmpty(elemId)
{
    var elem = document.getElementById(elemId);
    var value = yrprxUtil.trim(elem.value);

    if (!value || !value.length)
        doError(elem);
}

function onOK()
{
    try
    {
        checkNotEmpty("domain");
        checkNotEmpty("element_pattern");

        var rule = window.arguments[0];

        var domainElem = document.getElementById("domain");
        var domain = domainElem.value;
        domain = domain.replace(/^www\./i, "");
        if (!domain || !domain.length)
            doError(domainElem);
        rule.domain = domain.toLowerCase();

        if (!rule.page) // but page must not be null!
            rule.page = new wsshWebPageProfile();
        var page = rule.page;

        var pagePatternElem = document.getElementById("page_pattern");
        try
        {
            var pagePattern = yrprxUtil.trim(pagePatternElem.value);
            page.patternAsString = pagePattern ? pagePattern : ".*";
            page.pattern = new RegExp(page.patternAsString, "i");
        }
        catch(ex)
        {
            doRegExpError(pagePatternElem, ex);
        }

        var elementTag = document.getElementById("element_tag").selectedItem.value;
        if (page.elementTag && elementTag != page.elementTag) // reset elementUrlAttribute if it was set
            page.elementUrlAttribute = null;
        page.elementTag = elementTag;

        var elementPatternElem = document.getElementById("element_pattern");
        try
        {
            page.elementPatternAsString = elementPatternElem.value;
            page.elementPattern = new RegExp(elementPatternElem.value, "i");
        }
        catch(ex)
        {
            doRegExpError(elementPatternElem, ex);
        }

        page.elementTemplate = yrprxUtil.trim(document.getElementById("element_template").value);

        rule.page.disabled = false;

        rule.ok = true;
        return true;
    }
    catch(ex)
    {
        if (gLogger.enabled) gLogger.error("onOK: ", ex);
    }
    return false;
}
